﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using System.Threading;

namespace SmsCovert.Service
{
    class LOG
    {
        static string fUrl = @"D:\SMS.dat";
        FileInfo file = new FileInfo(fUrl);

        public LOG(DateTime dt)
        {
            if (!file.Exists)
            {
                File.Create(fUrl).Close();
               
                string message ="SMS\r\n" + dt.Year + "," + dt.Month + "," + dt.Day + "," + dt.Hour + "," + dt.Minute + "," + dt.Second;
                FileStream fs = file.OpenWrite();
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(message);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
        }

        public string LogReadFirst()
        {
            string text = "";
            FileStream fs = file.OpenRead();
            StreamReader sr = new StreamReader(fs);
            sr.ReadLine();
            text = sr.ReadLine();
            sr.Close();
            fs.Close();
            return text;
        }

        public string LogReadPrv()
        {
            string text = "";
            FileStream fs = file.OpenRead();
            StreamReader sr = new StreamReader(fs);
            sr.ReadLine();
            sr.ReadLine();
            text = sr.ReadLine();
            sr.Close();
            fs.Close();
            return text;
        }

        public void LogWrite(DateTime dt)
        {
            string prv = "SMS\r\n" + LogReadFirst() + "\r\n";
            string message = prv + dt.Year + "," + dt.Month + "," + dt.Day + "," + dt.Hour + "," + dt.Minute + "," + dt.Second;
            FileStream fs =  file.OpenWrite();
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(message);
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public int Worning()
        {
            DateTime dt_prv = getDatetime(LogReadPrv());
            TimeSpan days = dt_prv - DateTime.Now;
            return Convert.ToInt32(days.Days.ToString());
        }

        public bool IsNormal()
        {
            DateTime dt_prv = getDatetime(LogReadPrv());
            int res = dt_prv.CompareTo(DateTime.Now);
            if (res == -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int restDays()
        {
            DateTime first_dt = getDatetime(LogReadFirst());
            DateTime current_dt = DateTime.Now;
            TimeSpan days = current_dt - first_dt;
            return 15 - Convert.ToInt32(days.Days.ToString());
        }

        public DateTime getDatetime(string str)
        {
            string[] dts = str.Split(',');
            DateTime dt = new DateTime(Convert.ToInt32(dts[0]),Convert.ToInt32(dts[1]),Convert.ToInt32(dts[2]),Convert.ToInt32(dts[3]),Convert.ToInt32(dts[4]),Convert.ToInt32(dts[5]));
            return dt;
        }

    }
}
